<?php

namespace App\Services;

use App\Models\SwitchPort;
use Slowlyo\OwlAdmin\Services\AdminService;

/**
 * 交换机端口查看
 *
 * @method SwitchPort getModel()
 * @method SwitchPort|\Illuminate\Database\Query\Builder query()
 */
class SwitchPortService extends AdminService
{
	protected string $modelName = SwitchPort::class;

	public function list()
	{
		// 处理查询条件
		$query = $this->listQuery();

		// 分页查询数据
		$list  = $query->paginate(request()->input('perPage', 100));
		$items = $list->items();
		$total = $list->total();

		return compact('items', 'total');
	}

	public function searchable($query)
	{
		$query->with('switch');
		parent::searchable($query);

		$query->when($this->request->input('switchId'), fn($q) => $q->where('switch_id', $this->request->input('switchId')));
	}

	public function sortable($query){
		$query->orderBy('port_index', 'asc');
	}
}